﻿namespace Summoner
{
    using UnityEngine;
    using UnityEditor;
    using System.IO;
    using System.Collections.Generic;
    using System;

    public class ConsolePublisher
    {
        public static string AssetConfigName = "AssetNames";
        private static BuildTarget buildTarget = BuildTarget.iOS;
        private static BuildOptions option = BuildOptions.None;

        public static void Build()
        {
            #region check build parameters
            string[] args = Environment.GetCommandLineArgs();
            option = ConvertToBuildTarget<BuildOptions>(args[args.Length - 1]);
            buildTarget = ConvertToBuildTarget<BuildTarget>(args[args.Length - 2]);
            #endregion
            AssetVessel vessel = AssetDatabase.LoadAssetAtPath(AssetPath.AssetVesselPath, typeof(AssetVessel)) as AssetVessel;
            BuildBundle(vessel, buildTarget);
            Build(buildTarget);
        }

        private static void BuildBundle(AssetVessel vessel, BuildTarget buildTarget)
        {
            BundleFunction.GenerateAssetBundle(vessel.assetList, buildTarget);
            GameConfigInfo information = new GameConfigInfo() { version = vessel.version, platform = Application.platform, language = SystemLanguage.Chinese, assets = vessel.assetList };
            JsonSerializer.PackJsonToFile(AssetPath.PublishPath, information, AssetPath.AssetConfigName.ToLower());
        }

        private static T ConvertToBuildTarget<T>(string value)
        {
            return (T)Enum.Parse(typeof(T), value);
        }

        private static string GetCurrentDate()
        {
            return DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second;
        }

        private static string GetProjectName()
        {
            string[] s = Application.dataPath.Split('/');
            return (s[s.Length - 2] + "-" + GetCurrentDate());
        }

        private static string[] GetLevelsFromBuildSettings()
        {
            List<string> levels = new List<string>();
            for (int i = 0; i < EditorBuildSettings.scenes.Length; ++i)
            {
                if (EditorBuildSettings.scenes[i].enabled)
                {
                    levels.Add(EditorBuildSettings.scenes[i].path);
                }
            }
            return levels.ToArray();
        }

        private static void Build(BuildTarget buildTarget)
        {
            string[] scenes = GetLevelsFromBuildSettings();
            Directory.CreateDirectory(BuildPath());
            BuildOptions BuildOp = BuildOptions.None;
            switch (buildTarget)
            {
                case BuildTarget.iOS:
                    {
                        EditorUserBuildSettings.SwitchActiveBuildTarget(buildTarget);
                        BuildPipeline.BuildPlayer(scenes, BuildPath() + "-" + GetCurrentDate(), buildTarget, BuildOp);
                    }
                    break;
                case BuildTarget.Android:
                    {
                        EditorUserBuildSettings.SwitchActiveBuildTarget(buildTarget);
                        if ((option & BuildOptions.AcceptExternalModificationsToPlayer) == BuildOptions.AcceptExternalModificationsToPlayer)
                        {
                            BuildPipeline.BuildPlayer(scenes, BuildPath() + "/" + GetProjectName(), buildTarget, BuildOp);
                        }
                        else
                        {
                            BuildPipeline.BuildPlayer(scenes, BuildPath() + "/" + GetProjectName() + ".apk", buildTarget, BuildOp);
                        }
                    }
                    break;
                case BuildTarget.StandaloneWindows:
                    {
                        EditorUserBuildSettings.SwitchActiveBuildTarget(buildTarget);
                        BuildPipeline.BuildPlayer(scenes, BuildPath() + "/" + GetProjectName() + ".exe", buildTarget, BuildOp);
                    }
                    break;
                case BuildTarget.StandaloneWindows64:
                    {
                        EditorUserBuildSettings.SwitchActiveBuildTarget(buildTarget);
                        BuildPipeline.BuildPlayer(scenes, BuildPath() + "/" + GetProjectName() + ".exe", buildTarget, BuildOp);
                    }
                    break;
                case BuildTarget.StandaloneOSXIntel:
                    {
                        EditorUserBuildSettings.SwitchActiveBuildTarget(buildTarget);
                        BuildPipeline.BuildPlayer(scenes, BuildPath() + "/" + GetProjectName() + ".app", buildTarget, BuildOp);
                    }
                    break;
                case BuildTarget.StandaloneOSXIntel64:
                    {
                        EditorUserBuildSettings.SwitchActiveBuildTarget(buildTarget);
                        BuildPipeline.BuildPlayer(scenes, BuildPath() + "/" + GetProjectName() + ".app", buildTarget, BuildOp);
                    }
                    break;
                case BuildTarget.StandaloneOSXUniversal:
                    {
                        EditorUserBuildSettings.SwitchActiveBuildTarget(buildTarget);
                        BuildPipeline.BuildPlayer(scenes, BuildPath() + "/" + GetProjectName() + ".app", buildTarget, BuildOp);
                    }
                    break;
#if !UNITY_5
                case BuildTarget.WebPlayer:
                    {
                        EditorUserBuildSettings.SwitchActiveBuildTarget(buildTarget);
                        BuildPipeline.BuildPlayer(scenes, BuildPath() + "/" + GetProjectName(), buildTarget, BuildOp);
                    }
                    break;
                case BuildTarget.WebPlayerStreamed:
                    {
                        EditorUserBuildSettings.SwitchActiveBuildTarget(buildTarget);
                        BuildPipeline.BuildPlayer(scenes, BuildPath() + "/" + GetProjectName(), buildTarget, BuildOp);
                    }
                    break;
#endif
            }
        }
        private static string BuildPath()
        {
            switch (buildTarget)
            {
                case BuildTarget.iOS:
                    return "builds/IOS";
                case BuildTarget.Android:
                    return "builds/Android";
                case BuildTarget.StandaloneWindows:
                    return "builds/Win";
                case BuildTarget.StandaloneWindows64:
                    return "builds/Win64";
                case BuildTarget.StandaloneOSXIntel:
                    return "builds/Mac";
                case BuildTarget.StandaloneOSXIntel64:
                    return "builds/Mac64";
                case BuildTarget.StandaloneOSXUniversal:
                    return "builds/MacUniversal";
#if !UNITY_5
                case BuildTarget.WebPlayer:
                    return "builds/WebPlayer";
                case BuildTarget.WebPlayerStreamed:
                    return "builds/WebPlayerStream";
#endif
            }
            return "builds";
        }
        private static string ConfigPath
        {
            get
            {
                return Path.Combine(Application.dataPath, "AssetConfig");
            }
        }
    }
}